//
//  SSOverlapSwitchScaleAndRangeController.m
//  Investigation Catalyst
//
//  Created by Tobias Haughton on Sun Jan 11 2004.
//  Copyright (c) 2004 Starline Software Ltd. All rights reserved.
//

#import "SSOverlapSwitchScaleAndRangeController.h"
#import "SSCatalystDocument.h"

@implementation SSOverlapSwitchScaleAndRangeController

+ (int)displayOverlapSwitchScaleAndRangePanel {
   static SSOverlapSwitchScaleAndRangeController *sharedOverlapSwitchScaleAndRangePanelController = nil;

   if(!sharedOverlapSwitchScaleAndRangePanelController) {
      sharedOverlapSwitchScaleAndRangePanelController = [[SSOverlapSwitchScaleAndRangeController allocWithZone:[self zone]] init];
   }

   [sharedOverlapSwitchScaleAndRangePanelController showWindow:self];

   return [sharedOverlapSwitchScaleAndRangePanelController result];
}

- (id)init {
   self = [super initWithWindowNibName:@"OverlapSwitchScaleAndRangePanel"];
   if (self) {
      [self setWindowFrameAutosaveName:@"OverlapSwitchScaleAndRangePanel"];
   }
   return self;
}

- (IBAction)showWindow:(id)sender
{
   //display our panel in a model session
   NSModalSession session;

   needsUpdate = YES;
   
   [self window];

   session = [NSApp beginModalSessionForWindow:[self window]];
   for (;;) {
      if ([NSApp runModalSession:session] != NSRunContinuesResponse)
         break;
   }
   [NSApp endModalSession:session];
}

- (IBAction)displayOverlap:(id)sender
{
   //set all of the values
   SSCatalystDocument *currentDocument = [[NSDocumentController sharedDocumentController] currentDocument];

   [representedObject removeFilterWithIdentifier:[beginTimeField tag]];
   if([[beginTimeField objectValue] isKindOfClass:[NSCalendarDate class]]) {
      [representedObject addFilterWithIdentifier:[beginTimeField tag]
                                            type:SSStartsAfterDateFilterType
                                           value:[self beginTimeFromFields]];
   }

   [representedObject removeFilterWithIdentifier:[endTimeField tag]];
   if([[endTimeField objectValue] isKindOfClass:[NSCalendarDate class]]) {
      [representedObject addFilterWithIdentifier:[endTimeField tag]
                                            type:SSEndsBeforeDateFilterType
                                           value:[self endTimeFromFields]];
   }

   [currentDocument updateObjectVisibility];
   
   switch([[unitPopup selectedItem] tag]) {
      case SSMillisecondUnitKey:[currentDocument setOverlapUnit:SSMillisecondUnit];
         break;
      case SS100THSecondUnitKey:[currentDocument setOverlapUnit:SS100THSecondUnit];
         break;
      case SSSecondUnitKey:[currentDocument setOverlapUnit:SSSecondUnit];
         break;
      case SSMinuteUnitKey:[currentDocument setOverlapUnit:SSMinuteUnit];
         break;
      case SSHourUnitKey:[currentDocument setOverlapUnit:SSHourUnit];
         break;
      default:[currentDocument setOverlapUnit:SSDayUnit];
         break;
   }

   [currentDocument setOverlapPixelsPerUnit:[self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]]];

   result = SSActionApproved;

   [NSApp stopModal];
   [self close];
}

- (IBAction)cancel:(id)sender
{
   result = SSActionCanceled;

   [NSApp stopModal];
   [self close];
}

- (void)setControlValues
{
   if(representedObject) {
      [displayOverlapButton setEnabled:YES];
      [cancelButton setEnabled:YES];
   } else {
      //there is no document so disable the controls
      [displayOverlapButton setEnabled:NO];
      [cancelButton setEnabled:NO];
   }

   [super setControlValues];
}


- (int)result
{
   return result;
}

@end
